///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.style {
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.text.StyleSheet;
	import flash.utils.*;
	
	import mx.logging.ILogger;
	import mx.logging.Log;
	import mx.utils.ObjectUtil;
	
	import sugarcookie.collections.EnhancedArrayCollection;
	import sugarcookie.error.ApplicationError;
	import sugarcookie.style.vo.Style;
	
	/**
	 * Allows for the central management of StyleSheets and font embedding.
	 */
	public class StyleManager extends EventDispatcher {
		private static var _embed:Boolean = true;
		private static var _styles:EnhancedArrayCollection;
		
		protected var _log:ILogger;
		
		public function StyleManager() {
			super();
			_log = Log.getLogger("sugarcookie.style.StyleManager");
			_styles = new EnhancedArrayCollection();
			initStyles();
		}
		
		public static function get embed():Boolean {
			return _embed;
		}
		
		public static function set embed(b:Boolean):void {
			_embed = b;
		}
		
		public static function getStyleSheet(id:String):StyleSheet {
			if (!StyleManager._styles.containsKey(id)) throw new ApplicationError("Style ID does not exist.");
			return StyleSheet(StyleManager._styles.getItemAtKey(id));
		}
		
		public function addStyleSheet(id:String, css:Object):void {
			if (StyleManager._styles.containsKey(id)) throw new ApplicationError("Style ID already exists.  StyleSheet was not added!");
			
			var __name:String = XML(describeType(css)).@name;
			if (__name.indexOf("String")!=-1) {
				_log.debug("styleSheet argument is a url, attempting to load stylesheet.");
				loadCSSFile(id,css as String);
			} else if (__name.indexOf("Array")!=-1) {
				_log.debug("styleSheet argument is an array, attempting to itterate through style objects to create style sheet.");
				parseStyleArray(id,css as Array);
			} else if (__name.indexOf("StyleSheet")!=-1) {
				_log.debug("styleSheet argument is a StyleSheet, adding to list.");
				_styles.addItemWithKey(id,StyleSheet(css));
			}
		}
		
		protected function initStyles():void {
			_log.info("StyleManager.initStyles() can be overridden in the sub-class to facilitate the creation of styles and stylesheets.");
		}
		
		private function loadCSSFile(id:String,cssURI:String):void {
			//TODO: finish this functionality, not needed right now.
		}
		
		private function parseStyleArray(id:String,styles:Array):void {
			var ss:StyleSheet = new StyleSheet();
			
			for (var i:int=0;i<styles.length;i++) {
				if (XML(describeType(styles[i])).@name!="sugarcookie.style.vo::Style") throw new ApplicationError("Not a valid sugarcookie.style.vo.Style object, unable to parse.");
				ss.setStyle(Style(styles[i]).name,Style(styles[i]).style);
			}
			
			StyleManager._styles.addItemWithKey(id,ss);
		}
	}
}